"use strict";
cc._RF.push(module, 'b4f06bMKddJh4jxlZJ7rcMt', 'PopupBase');
// eazax-ccc/components/popups/PopupBase.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
/**
 * 弹窗基类
 * @see PopupBase.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/components/popups/PopupBase.ts
 * @see PopupManager.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/core/PopupManager.ts
 * @version 20210409
 */
var PopupBase = /** @class */ (function (_super) {
    __extends(PopupBase, _super);
    function PopupBase() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.background = null;
        _this.main = null;
        /** 展示/隐藏动画的时长 */
        _this.animDuration = 0.3;
        /** 用于拦截点击的节点 */
        _this.blocker = null;
        /** 弹窗选项 */
        _this.options = null;
        /**
         * 弹窗流程结束回调（注意：该回调为 PopupManager 专用，重写 hide 函数时记得调用该回调）
         */
        _this.finishCallback = null;
        return _this;
    }
    /**
     * 展示弹窗
     * @param options 弹窗选项
     * @param duration 动画时长
     */
    PopupBase.prototype.show = function (options, duration) {
        var _this = this;
        if (duration === void 0) { duration = this.animDuration; }
        return new Promise(function (res) {
            // 储存选项
            _this.options = options;
            // 初始化节点
            var background = _this.background, main = _this.main;
            _this.node.active = true;
            background.active = true;
            background.opacity = 0;
            main.active = true;
            main.scale = 0.5;
            main.opacity = 0;
            // 初始化
            _this.init(_this.options);
            // 更新样式
            _this.updateDisplay(_this.options);
            // 播放背景遮罩动画
            cc.tween(background)
                .to(duration * 0.8, { opacity: 200 })
                .start();
            // 播放弹窗主体动画
            cc.tween(main)
                .to(duration, { scale: 1, opacity: 255 }, { easing: 'backOut' })
                .call(function () {
                // 弹窗已完全展示
                _this.onShow && _this.onShow();
                // Done
                res();
            })
                .start();
        });
    };
    /**
     * 隐藏弹窗
     * @param suspended 是否被挂起
     * @param duration 动画时长
     */
    PopupBase.prototype.hide = function (suspended, duration) {
        var _this = this;
        if (suspended === void 0) { suspended = false; }
        if (duration === void 0) { duration = this.animDuration; }
        return new Promise(function (res) {
            var node = _this.node;
            // 动画时长不为 0 时拦截点击事件（避免误操作）
            if (duration !== 0) {
                var blocker = _this.blocker;
                if (!blocker) {
                    blocker = _this.blocker = new cc.Node('blocker');
                    blocker.addComponent(cc.BlockInputEvents);
                    blocker.setParent(node);
                    blocker.setContentSize(node.getContentSize());
                }
                blocker.active = true;
            }
            // 播放背景遮罩动画
            cc.tween(_this.background)
                .delay(duration * 0.2)
                .to(duration * 0.8, { opacity: 0 })
                .start();
            // 播放弹窗主体动画
            cc.tween(_this.main)
                .to(duration, { scale: 0.5, opacity: 0 }, { easing: 'backIn' })
                .call(function () {
                // 关闭拦截
                _this.blocker && (_this.blocker.active = false);
                // 关闭节点
                node.active = false;
                // 弹窗已完全隐藏（动画完毕）
                _this.onHide && _this.onHide(suspended);
                // Done
                res();
                // 弹窗完成回调
                _this.finishCallback && _this.finishCallback(suspended);
            })
                .start();
        });
    };
    /**
     * 初始化（派生类请重写此函数以实现自定义逻辑）
     */
    PopupBase.prototype.init = function (options) { };
    /**
     * 更新样式（派生类请重写此函数以实现自定义样式）
     * @param options 弹窗选项
     */
    PopupBase.prototype.updateDisplay = function (options) { };
    /**
     * 弹窗已完全展示（派生类请重写此函数以实现自定义逻辑）
     */
    PopupBase.prototype.onShow = function () { };
    /**
     * 弹窗已完全隐藏（派生类请重写此函数以实现自定义逻辑）
     * @param suspended 是否被挂起
     */
    PopupBase.prototype.onHide = function (suspended) { };
    /**
     * 设置弹窗完成回调（该回调为 PopupManager 专用）
     * @param callback 回调
     */
    PopupBase.prototype.setFinishCallback = function (callback) {
        this.finishCallback = callback;
    };
    __decorate([
        property({ type: cc.Node, tooltip: CC_DEV && '背景遮罩' })
    ], PopupBase.prototype, "background", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: CC_DEV && '弹窗主体' })
    ], PopupBase.prototype, "main", void 0);
    PopupBase = __decorate([
        ccclass
    ], PopupBase);
    return PopupBase;
}(cc.Component));
exports.default = PopupBase;

cc._RF.pop();